package com.css.fxfzaqbz.modules.assess.controller;

import com.css.fxfzaqbz.base.response.RestResponse;
import com.css.fxfzaqbz.base.upload.entity.AttachmentInfoEntity;
import com.css.fxfzaqbz.base.upload.service.AttachmentInfoService;
import com.css.fxfzaqbz.modules.assess.entity.*;
import com.css.fxfzaqbz.modules.assess.service.SecondDisastersService;
import com.css.fxfzaqbz.modules.assess.vo.DisasterAssVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.*;

/**
 * @author y1fan
 * @create 2022-09-14 17:53
 * 次生灾害隐患
 */
@RestController
@RequestMapping("/fxfzaqbz/data")
@Slf4j
public class SecondDisastersController {


    @Resource
    private SecondDisastersService secondDisastersService;

    @Resource
    private AttachmentInfoService attachmentInfoService;


    /*
    * 添加表单上传
    * */
    @PostMapping("/saveSecondDisasters")
    public RestResponse saveSecondDisasters(@RequestBody SecondDisastersEntity secondDisastersEntity) {
        RestResponse restResponse = null ;

        try{
            restResponse = RestResponse.succeed(secondDisastersService.saveSecondDisasters(secondDisastersEntity));
        }catch (Exception e){
            log.error("新增失败",e);
            restResponse = RestResponse.fail("新增失败!");
        }
        return restResponse;
    }

    /*
    * 查询次生灾害隐患表
    * id为活动id
    * 过程稿
    * */
    @GetMapping("/getAllSecondDisasters")
    public RestResponse getAllSecondDisasters(String id){
        RestResponse restResponse = null;
        try{
          restResponse = RestResponse.succeed( secondDisastersService.getAllSecondDisasters(id));
        }catch (Exception e){
            log.error("查询数据失败!",e);
            restResponse = RestResponse.fail("查询数据失败");
        }
        return restResponse;
    }




    /*
     * 查询次生灾害隐患表
     * id为活动id
     * 最终成果稿
     * */
    @GetMapping("/getAllSecondDisastersTwo")
    public RestResponse getAllSecondDisastersTwo(String id){
        RestResponse restResponse = null;
        try{
            restResponse = RestResponse.succeed( secondDisastersService.getAllSecondDisastersTwo(id));
        }catch (Exception e){
            log.error("查询数据失败!",e);
            restResponse = RestResponse.fail("查询数据失败");
        }
        return restResponse;
    }



    /*
    * 根据ID 查询上传成果
    * */
    @GetMapping("/getSecondDisastersById")
    public RestResponse<CasualtiesEntity> getSecondDisastersById(String id) {
        RestResponse restResponse = null;
        try{
            SecondDisastersEntity secondDisastersById = secondDisastersService.getSecondDisastersById(id);
            Map<String,Object> map=new HashMap<>();
            if (!("").equals(secondDisastersById.getSsdAreaFileId())&&secondDisastersById.getSsdAreaFileId()!=null){
                AttachmentInfoEntity areaAttachment = attachmentInfoService.getAttach(secondDisastersById.getSsdAreaFileId());
                Map<String, Object> excelByArea = secondDisastersService.getExcelByArea(areaAttachment.getAttachId());
                map.put("excelByArea",excelByArea);
                map.put("areaAttachment",areaAttachment);

            }
            if (!("").equals(secondDisastersById.getSsdImageId())&&secondDisastersById.getSsdImageId()!=null){
                AttachmentInfoEntity imageAttachment = attachmentInfoService.getAttach(secondDisastersById.getSsdImageId());
                map.put("imageAttachment",imageAttachment);
            }
            if (!("").equals(secondDisastersById.getSsdMouseFileId())&&secondDisastersById.getSsdMouseFileId()!=null){
                AttachmentInfoEntity mouseAttachment = attachmentInfoService.getAttach(secondDisastersById.getSsdMouseFileId());
                Map<String, Object> excelByMouth = secondDisastersService.getExcelByMouth(mouseAttachment.getAttachId());
                map.put("excelByMouth",excelByMouth);
                map.put("mouseAttachment",mouseAttachment);
            }

            map.put("secondDisastersById",secondDisastersById);
            restResponse = RestResponse.succeed(map);
        }catch (Exception e){
            log.error("获取数据失败!",e);
            restResponse = RestResponse.fail("获取数据失败!");
        }
        return restResponse;
    }

    /*
    * 删除上传成果
    * */
    @PostMapping("/deleteSecondDisasters")
    public RestResponse deleteSecondDisasters(String id){
        try {
            secondDisastersService.deleteSecondDisasters(id);
            return RestResponse.succeed("删除成功");
        } catch (Exception e) {
            log.error("删除失败",e);
            return RestResponse.fail("删除失败");
        }
    }

    /*
    * 修改上传成果
    * */
    @PostMapping("/updateSecondDisasters")
    public RestResponse updateSecondDisasters(@RequestBody SecondDisastersEntity secondDisastersEntity){
        RestResponse restResponse = null;
        try {

            SecondDisastersEntity secondDisastersEntity1=  secondDisastersService.updateSecondDisasters(secondDisastersEntity);
            restResponse=RestResponse.succeed(secondDisastersEntity1);
        } catch (Exception e) {
            log.error("修改失败",e);
            return RestResponse.fail("修改失败");
        }
        return restResponse;
    }


    @GetMapping("/getExcelByMouth")
    public RestResponse getExcelByMouth(String id)  {
        RestResponse restResponse = null;
        try {
        Map<String,Object> map= secondDisastersService.getExcelByMouth(id);

        restResponse=RestResponse.succeed(map);
        }catch (Exception e){
            restResponse = RestResponse.fail("表格格式错误");
        }

        return restResponse;
    }




    /*
    * id为文件id
    * */
    @GetMapping("/getExcelByArea")
    public RestResponse getExcelByArea(String id) throws Exception{
        RestResponse restResponse = null;
        Map<String,Object> map= null;
        try {
            map = secondDisastersService.getExcelByArea(id);
            restResponse = RestResponse.succeed(map);
        }catch (Exception e){
            restResponse = RestResponse.fail("表格格式错误");
        }


        return restResponse;
    }


    /*
     * 过程稿提交到最终成果接口
     * */
    @PostMapping("/firstSubmitSecondDisasters")
    public RestResponse firstSubmitSecondDisasters(String id){
        try {
            secondDisastersService.firstSubmitSecondDisasters(id);
            return RestResponse.succeed("提交成功");
        } catch (Exception e) {
            log.error("提交失败",e);
            return RestResponse.fail("提交失败");
        }
    }



    /*
     * 是否重复接口
     * */
    @PostMapping("/secondDisastersIfRepetition")
    public RestResponse SecondDisastersIfRepetition(@RequestBody SecondDisastersEntity secondDisastersEntity){

        try {
            Boolean flag=secondDisastersService.SecondDisastersIfRepetition(secondDisastersEntity);
            return RestResponse.succeed(flag);
        } catch (Exception e) {
            log.error("校验失败",e);
            return RestResponse.fail("校验失败");
        }
    }



    /*
     * 添加描述接口
     * */
    @PostMapping("/addSecondDisastersDisaster")
    public RestResponse addSecondDisastersDisaster(@RequestBody DisasterAssVO disasterAssVO){
        try {
            String describe="";
            if (disasterAssVO.getDisasterAss()!=null && disasterAssVO.getDisasterAss().size()!=0){
                describe = StringUtils.join(disasterAssVO.getDisasterAss(), "@@@");
            }

            secondDisastersService.addSecondDisastersDisaster(disasterAssVO.getIds(),describe);
            return RestResponse.succeed("添加描述成果");
        } catch (Exception e) {
            log.error("添加描述失败",e);
            return RestResponse.fail("添加描述失败");
        }
    }


    /*
     * 移除接口
     * */
    @PostMapping("/removeSecondDisasters")
    public RestResponse removeSecondDisasters(String id){
        try {
            secondDisastersService.removeSecondDisasters(id);
            return RestResponse.succeed("移除成功");
        } catch (Exception e) {
            log.error("移除失败",e);
            return RestResponse.fail("移除失败");
        }
    }

    /*
     * 最终成果提交接口
     * */
    @PostMapping("/secondSubmitSecondDisasters/{ids}")
    public RestResponse secondSubmitSecondDisasters(@PathVariable("ids") List<String> ids){
        try {
            secondDisastersService.secondSubmitSecondDisasters(ids);
            return RestResponse.succeed("提交成功");
        } catch (Exception e) {
            log.error("提交失败",e);
            return RestResponse.fail("提交失败");
        }
    }




}
